﻿using System.Windows.Input;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using HTR.Model;
using MvvmFoundation.Wpf;

namespace HTR.ViewModels
{
    public class BocaRegistroViewModel : ObservableObject
    {

        #region Propiedades

        private string _cotaTerrenoAnterior;
        private bool _existenteAnterior;
        private string _selectedHandle;

        private string _cotaTerrenoText;
        public string CotaTerrenoText
        {
            get { return _cotaTerrenoText; }
            set { _cotaTerrenoText = value; RaisePropertyChanged("CotaTerrenoText"); }
        }

        private bool _existente;
        public bool Existente
        {
            get { return _existente; }
            set { _existente = value; CotaTerrenoIsEnabled = !value; RaisePropertyChanged("Existente");  }
        }
        
        private bool _cotaTerrenoIsEnabled;
        public bool CotaTerrenoIsEnabled
        {
            get { return _cotaTerrenoIsEnabled; }
            private set { _cotaTerrenoIsEnabled = value; RaisePropertyChanged("CotaTerrenoIsEnabled"); }
        }

        private bool _existenteIsEnabled;
        public bool ExistenteIsEnabled
        {
            get { return this._existenteIsEnabled; }
            private set { this._existenteIsEnabled = value; RaisePropertyChanged("ExistenteIsEnabled"); }
        }

        private bool _insertarIsEnabled;
        public bool InsertarIsEnabled
        {
            get { return _insertarIsEnabled; }
            private set { _insertarIsEnabled = value; RaisePropertyChanged("InsertarIsEnabled"); }
        }

        private bool _modificarIsEnabled;
        public bool ModificarIsEnabled
        {
            get { return _modificarIsEnabled; }
            private set { _modificarIsEnabled = value; RaisePropertyChanged("ModificarIsEnabled"); }
        }

        private System.Windows.Visibility _editVisibility;
        public System.Windows.Visibility EditVisibility
        {
            get { return _editVisibility; }
            private set { _editVisibility = value; RaisePropertyChanged("EditVisibility"); }
        }

        private System.Windows.Visibility _nonEditVisibility;
        public System.Windows.Visibility NonEditVisibility
        {
            get { return _nonEditVisibility; }
            private set { _nonEditVisibility = value; RaisePropertyChanged("NonEditVisibility"); }
        }

        private BocaRegistroCollection _bocasRegistro;
        public BocaRegistroCollection BocasRegistro
        {
            get { return this._bocasRegistro; }
        }

        public ICommand InsertarBocaRegistroCmd
        {
            get { return new RelayCommand(this.InsertarBocaRegistro); }
        }

        public ICommand ModificarBocaRegistroCmd
        {
            get { return new RelayCommand(this.ModificarBocaRegistro); }
        }

        public ICommand AceptarModifBocaRegistroCmd
        {
            get { return new RelayCommand(this.Aceptar); }
        }

        public ICommand CancelarModifBocaRegistroCmd
        {
            get { return new RelayCommand(this.Cancelar); }
        }

        #endregion // Propiedades


        #region Constructores

        public BocaRegistroViewModel()
        {
            this._bocasRegistro = new BocaRegistroCollection();
            this._cotaTerrenoText = string.Empty;
            this._cotaTerrenoIsEnabled = true;
            this._existente = false;
            this._existenteIsEnabled = true;
            this._insertarIsEnabled = true;
            this._modificarIsEnabled = true;
            EditVisibility = System.Windows.Visibility.Collapsed;
            NonEditVisibility = System.Windows.Visibility.Visible;
        }

        #endregion


        #region Metodos

        public void InsertarBocaRegistro()
        {
            if (!Existente)
            {
                double res = 0;
                if (!double.TryParse(CotaTerrenoText, out res) && !(CotaTerrenoText == string.Empty))
                {
                    Application.ShowAlertDialog("Por favor ingrese un valor numérico válido como Cota de Terreno.");
                    return;
                }
                if (res < 0)
                {
                    Application.ShowAlertDialog("El valor de la Cota de Terreno no puede ser negativo.");
                    return;
                }
            }

            DisableInput();

            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            PromptPointOptions pPtOpts = new PromptPointOptions("");
            pPtOpts.Message = "\nIngrese el punto de inserción: ";
            Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView();
            PromptPointResult pPtRes = acDoc.Editor.GetPoint(pPtOpts);
            if (pPtRes.Status == PromptStatus.Cancel)
            {
                EnableInput();
                return;
            }
            BocaRegistro br;
            if (Existente)
                br = new BocaRegistro(pPtRes.Value, "BRE", true);
            else
                br = new BocaRegistro(pPtRes.Value, CotaTerrenoText, false);
            br.Dibujar();
            if (!Existente) BocasRegistro.Add(br.Handle, br);
            CotaTerrenoText = string.Empty;
            EnableInput();
            return;
        }

        public void ModificarBocaRegistro()
        {
            DisableInput();
            this._cotaTerrenoAnterior = CotaTerrenoText;
            this._existenteAnterior = Existente;

            Existente = false;

            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            PromptEntityOptions pEnOpts = new PromptEntityOptions("");
            pEnOpts.Message = "\nSeleccione la Boca de Registro a modificar: ";
            // Acá van los filtros para que no se pueda seleccionar cualquier cosa
            Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView();
            PromptEntityResult pEnRes = acDoc.Editor.GetEntity(pEnOpts);
            if (pEnRes.Status == PromptStatus.Cancel)
            {
                CotaTerrenoText = this._cotaTerrenoAnterior;
                Existente = this._existenteAnterior;
                EditVisibility = System.Windows.Visibility.Collapsed;
                NonEditVisibility = System.Windows.Visibility.Visible;
                EnableInput();
                return;
            }
            try
            {
                this._selectedHandle = pEnRes.ObjectId.Handle.ToString();
                BocaRegistro br = BocasRegistro[this._selectedHandle];
                CotaTerrenoText = br.ValorCotaTerreno;
                EditVisibility = System.Windows.Visibility.Visible;
                NonEditVisibility = System.Windows.Visibility.Collapsed;
            }
            catch
            {
                Application.ShowAlertDialog("Por favor seleccione una Boca de Registro.");
            }
            finally
            {
                EnableInput();
                ExistenteIsEnabled = false;
            }
            return;
        }

        public void Aceptar()
        {
            BocaRegistro br = BocasRegistro[this._selectedHandle];

            br.Modificar(CotaTerrenoText);

            EditVisibility = System.Windows.Visibility.Collapsed;
            NonEditVisibility = System.Windows.Visibility.Visible;
            CotaTerrenoText = string.Empty;
            ExistenteIsEnabled = true;
        }

        public void Cancelar()
        {
            CotaTerrenoText = this._cotaTerrenoAnterior;
            Existente = this._existenteAnterior;
            EditVisibility = System.Windows.Visibility.Collapsed;
            NonEditVisibility = System.Windows.Visibility.Visible;
            ExistenteIsEnabled = true;
        }

        private void EnableInput()
        {
            InsertarIsEnabled = true;
            ModificarIsEnabled = true;
            CotaTerrenoIsEnabled = !Existente;
            ExistenteIsEnabled = true;
        }

        private void DisableInput()
        {
            InsertarIsEnabled = false;
            ModificarIsEnabled = false;
            CotaTerrenoIsEnabled = false;
            ExistenteIsEnabled = false;
        }

        #endregion // Metodos

    }
}
